package com.glory.eapequipmentmonitor.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.adapter.ContactAdapter;
import com.glory.eapequipmentmonitor.bean.ContactsInfoBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ContactFragment extends FreeUI_BaseFragment implements OnRefreshListener {
    @BindView(R.id.rvContact)
    RecyclerView rvContact;
    @BindView(R.id.userRefreshView)
    SmartRefreshLayout userRefreshView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;


    private ContactAdapter contactAdapter;
    private List<ContactsInfoBean> userList;

    public ContactFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_contact;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void initView() {
        xRefreshView = userRefreshView;
        rvContact.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvContact.setItemAnimator(new DefaultItemAnimator());
        rvContact.addItemDecoration(new DividerItemDecoration(getActivity(), 1));
        contactAdapter = new ContactAdapter(getActivity(), getFragmentManager(), null);
        contactAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        contactAdapter.isFirstOnly(true);
        rvContact.setAdapter(contactAdapter);
        userRefreshView.setOnRefreshListener(this);
        userRefreshView.setEnableLoadmore(false);
//        rvContact.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    Glide.with(getActivity()).resumeRequests();//恢复Glide加载图片
//                } else {
//                    Glide.with(getActivity()).pauseRequests();//禁止Glide加载图片
//                }
//            }
//        });
        initSearchView();
    }

    private void initSearchView() {

        toolbar.inflateMenu(R.menu.toolbar_menu);
        SearchView searchView = toolbar.findViewById(R.id.action_search_kl);
        //改变默认的搜索图标
        ((ImageView) searchView.findViewById(R.id.search_button)).setImageResource(R.drawable.search);
        ((ImageView) searchView.findViewById(R.id.search_close_btn)).setImageResource(R.drawable.close);

        TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);

        final int textSize = (int) (textView.getTextSize() * 1.25);
        Drawable mSearchHintIcon = getResources().getDrawable(R.drawable.search);
        mSearchHintIcon.setBounds(0, 0, textSize, textSize);
        final SpannableStringBuilder ssb = new SpannableStringBuilder("   ");
        ssb.setSpan(new ImageSpan(mSearchHintIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.append("请输入搜索文字");
        textView.setHint(ssb);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);//14sp
        textView.setHintTextColor(getResources().getColor(R.color.free_ui_glory_bg));
        textView.setTextColor(Color.WHITE);
        //搜索监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchData(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                searchData(newText);
                return true;
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                contactAdapter.setNewData(userList);
                return false;
            }
        });
    }

    private void searchData(String newText) {
        List<ContactsInfoBean> userSearchList =new ArrayList<>();
        for (ContactsInfoBean contactsInfoBean : userList){
            if (contactsInfoBean.getCname().contains(newText)){
                userSearchList.add(contactsInfoBean);
            }
        }
        contactAdapter.setNewData(userSearchList);
    }

    @Override
    protected void initData() {
        userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ContactsInfoBean bean = new ContactsInfoBean();
            bean.setAcronym("ZS" + i);
            bean.setCname("张三" + i);
            bean.setDepartment("虚拟部门" + i);
            userList.add(bean);
        }
        contactAdapter.setNewData(userList);
    }

//    /**
//     * 获取数据 先查询本地数据库有无数据，没有则请求服务器并保存数据库
//     */
//    private void getData() {
//
//        NetUtils.getInstance().getMailList(new FreeUI_DialogEntityCallBack<BaseResponseBean<List<ContactsInfoBean>>>(
//                new TypeToken<BaseResponseBean<List<ContactsInfoBean>>>() {
//                }.getType(), getFragmentManager(), this) {
//            @Override
//            public void onSuccess(Response<BaseResponseBean<List<ContactsInfoBean>>> response) {
//                if (response.body().isHaveContent(App.app)) {
//                    userList = response.body().getData();
//                    contactAdapter.setNewData(userList);
//                    userRefreshView.finishRefresh(true);
//                    storageData();
//                }
//            }
//
//            @Override
//            public void onError(Response<BaseResponseBean<List<ContactsInfoBean>>> response) {
//                super.onError(response);
//                loadError(response.getException(), "ContactFragment");
//            }
//        }, 0, 0, this);
//    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {

        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loadComplete(true);
            }
        }, 1000);
    }

}
